package pl.edu.agh.iosr.gamblingzone.userpanel.action;

import static org.jboss.seam.ScopeType.EVENT;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

import pl.edu.agh.iosr.gamblingzone.model.Bet;
import pl.edu.agh.iosr.gamblingzone.model.BetOpaque;
import pl.edu.agh.iosr.gamblingzone.model.BettingSlip;

@Stateful
@Name("betsFromSlip")
@Scope(EVENT)
@Restrict("#{identity.loggedIn}")
public class BetViewAction implements BetView {

	@PersistenceContext
	private EntityManager em;
	
	@In(required=false) @Out
	private BettingSlip slip;
	
	@In
	private FacesMessages facesMessages;
	
	@Logger
	Log log;
	
	private Collection<Bet> bets;
	
	@DataModel(scope=ScopeType.PAGE)
	private List<BetOpaque> betsOpaque;
	
	public List<BetOpaque> selectBettingSlip(BettingSlip bettingSlip) {
		bettingSlip = em.merge(bettingSlip);
		
		bets = bettingSlip.getBets();
		betsOpaque = new LinkedList<BetOpaque>();
		
		for (Bet bet : bets) {
			em.refresh(bet);
			BetOpaque betOp = new BetOpaque(bet);			
			betsOpaque.add(betOp);
		}
		
		return null;
	}
	
	@Remove
	public void destroy() {
	}

}
